﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.IO;
using System.Windows.Threading;

namespace RenjianImageViewer.ViewModel
{
    class RenjianViewModel : DependencyObject
    {
        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register(
            "IsBusy",
            typeof(bool),
            typeof(RenjianViewModel),
            new FrameworkPropertyMetadata(false));

        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        public static readonly DependencyProperty CurrentImageProperty = DependencyProperty.Register(
            "CurrentImage",
            typeof(ImageViewModel),
            typeof(RenjianViewModel),
            new FrameworkPropertyMetadata(null));

        public ImageViewModel CurrentImage
        {
            get { return (ImageViewModel)GetValue(CurrentImageProperty); }
            set { SetValue(CurrentImageProperty, value); }
        }

        protected Dispatcher currentDispatcher;
        public DataModel.StatusModel StatusDataMoel { get; private set; }
        public event PropertyChangedCallback PropertyChanged;

        public ObservableCollection<ImageViewModel> Images { get; /*private*/ set; }
        public ObservableCollection<ImageViewModel> ConversitionImages { get; /*private*/ set; }
        public ObservableCollection<ImageViewModel> NewImages { get; /*private*/ set; }
        public SortedList<long, ImageViewModel> PreloadImageCache { get; set; }

        private DateTime lastMark = DateTime.Today.AddHours(DateTime.Now.Hour + 1);

        public RenjianViewModel()
        {
            currentDispatcher = Dispatcher.CurrentDispatcher;
            Images = new ObservableCollection<ImageViewModel>();
            ConversitionImages = new ObservableCollection<ImageViewModel>();
            NewImages = new ObservableCollection<ImageViewModel>();
            PreloadImageCache = new SortedList<long, ImageViewModel>();
        }

        public void AddImage/*Command*/(ImageViewModel image)
        {
            System.Diagnostics.Debug.Assert(Images.Count == 0 || image.StatusID < Images.Last().StatusID);
            var tmp = DateTime.Parse(image.StatusModel.CreateTime);
            var time = tmp.Date.AddHours(tmp.Hour + 1);
            if ((lastMark - time).TotalHours > 0)
            {
                lastMark = time;
                image.TimeTag = lastMark.ToString("MM/dd HH") + ":00 >>";
            }

            Images.Add(image);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            var tmp = PropertyChanged;
            if (tmp != null)
            {
                tmp(this, e);
            }
        }

        public void UpdateImagesIsLocalStatus()
        {
            long temp = 0;
            var conversitionIDs = from imageModel in ConversitionImages
                                  select imageModel.StatusID;

            var idList = conversitionIDs.ToList();

            foreach (var image in Images)
            {
                if (idList.Contains(image.StatusID))
                {
                    image.IsLocal = true;
                    idList.Remove(image.StatusID);
                    if (idList.Count == 0)
                    {
                        break;
                    }
                }
            }
        }
    }
}
